package com.homwee.factory;


import com.homwee.factory.util.BarCodeUtils;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import com.mediatek.twoworlds.factory.MtkTvFApiSystem;

public class BarCodeService extends Service {
	private static final String TAG = "BarCodeService";
	private BroadcastReceiver stopBroadcastReceiver = null;
        private String ESNIDFile = "/vendor/tvcertificate/netflix50/ESNID";
        private String showItem = "MAC";
        private final String mBrand = android.os.Build.BRAND;
		private final String mBuildProduct = android.os.Build.PRODUCT;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		startForeground(0,null);
        showItem = intent.getStringExtra("showItem");
		showBarCodePop();//show barcode
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.homwee.macbarcode.stopservice");
		stopBroadcastReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				Log.d(TAG, "cz  cyf test BarCodeService stopSelf");
				BarCodeUtils.removePopupWindow();
				stopSelf();
			}
		};
		registerReceiver(stopBroadcastReceiver, filter);
		return START_NOT_STICKY;//START_STICKY;
	}
	private void showBarCodePop(){
		String WifiMacAddr = "00:00:00:00:00:00";
		String MacAddr = "00:00:00:00:00:00";
		String BarCode = "000000";
        String ESNID = "No ESNID";
		
		//WifiMacAddr =  BarCodeUtils.getMAC("wlan0");
		WifiMacAddr=BarCodeUtils.getDeviceMac(this.getApplicationContext());
		MacAddr = BarCodeUtils.getMAC("eth0");
        if(MtkTvFApiSystem.getInstance().isFileExist(ESNIDFile)){
        	ESNID = MtkTvFApiSystem.getInstance().readFileOneLine(ESNIDFile, 100, 0);
        }

		String BarCodeUrl = "WIFI MAC\\" + WifiMacAddr + "\n" + "LAN MAC\\" + MacAddr + "\n" + "BarCode\\" + BarCode;
		if(mBuildProduct.contains("ISDB") && mBrand.equals("Philco")){
			if(showItem!= null && showItem.equals("ESN")){
				BarCodeUrl = ESNID;
			}
		}
		//Log.d(TAG, "cz cyf test BarCodeUrl = " + BarCodeUrl);
		
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();  
        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;  
        int mflags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
        		| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;  
        // | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;    
        params.flags = mflags;  
        params.format = PixelFormat.TRANSLUCENT;  
        params.width = LayoutParams.WRAP_CONTENT;  
        params.height = LayoutParams.WRAP_CONTENT;  
        params.gravity = Gravity.RIGHT|Gravity.BOTTOM; 
		BarCodeUtils.showPopupWindow(getApplicationContext(), BarCodeUtils.createQRImage(BarCodeUrl), params);
		
	}
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.d(TAG, "cz cyf test BarCodeService onDestroy");
		if(stopBroadcastReceiver != null)
			unregisterReceiver(stopBroadcastReceiver);
		//android.os.Process.killProcess(android.os.Process.myPid());
		super.onDestroy();
	}

}
